package com.huiquan.icd.dao;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.framework.constant.BaseContants;
import com.huiquan.icd.domain.IcdDisease;
import com.huiquan.icd.domain.IcdDiseaseDto;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class IcdDiseaseDao extends EnhancedBaseDao<IcdDisease> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "ICD_DISEASE.";

	public IcdDiseaseDao() {
		super(space);
	}

	public IcdDisease retrieveObject(String name, String disable, Integer hiddenFlag) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		param.put("disable", disable);
		param.put("Flag", hiddenFlag);
		return (IcdDisease) this.object(space + "RETRIEVE_OBJECT", param, sqlMapClient);
	}

	public void disable(Long id, Integer disable) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("disable", disable);
		if (disable == 1) {
			param.put("hiddenFlag", 1);
		}
		update(space + "DISABLE", param, sqlMapClient);
	}

	public String retrieveCategoryByName(String name) {
		return (String) this.object(space + "RETRIEVE_CATEGORY_BY_NAME", name, sqlMapClient);
	}

	public IcdDisease retrieveCategoryByName(String name, String id) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		param.put("nid", id);
		return (IcdDisease) this.object(space + "RETRIEVE_OBJECT_BY_NAME", param, sqlMapClient);
	}

	public List<IcdDisease> retrieveValidListByNameArray(List<String> wordArray) {
		Map<String, Object> param = new HashMap<>();
		param.put("wordArray", wordArray);
		param.put("disable", BaseContants.DISABLE_VALID);
		param.put("codeValid", wordArray);

		return retrieveList(param);
	}

	public void updateCategory(Long oldId, Long newId) {
		Map<String, Object> param = new HashMap<>();
		param.put("oldId", oldId);
		param.put("newId", newId);
		update(space + "UPDATE_CATEGORY", param, sqlMapClient);
	}

	public void setSex(String id, String sex, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("sex", sex);
		param.put("userId", user.getUserId());
		this.update(space + "SET_SEX", param, sqlMapClient);
	}

	public void setAge(String id, String age, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("age", age);
		param.put("userId", user.getUserId());
		this.update(space + "SET_AGE", param, sqlMapClient);
	}

	public void submitSexAge(String ids, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("ids", "(" + ids + ")");
		param.put("userId", user.getUserId());
		this.update(space + "SUBMIT_SEX_AGE", param, sqlMapClient);
	}

	public void setUrgentFlag(String id, String urgentFlag, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("urgentFlag", urgentFlag);
		param.put("userId", user.getUserId());
		this.update(space + "SET_URGENT_FLAG", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IcdDiseaseDto> retrieveDtoList(Map<String, Object> param) {
		return (List<IcdDiseaseDto>) this.list(space + "RETRIEVE_LIST", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<IcdDiseaseDto> retrieveDto2List(Map<String, Object> param) {
		return (List<IcdDiseaseDto>) this.list(space + "RETRIEVE_SEX_AGE_LIST", param, sqlMapClient);
	}

	public int retrieveDiByName(String name) {
		Map<String, Object> param = new HashMap<>();
		param.put("name", name);
		Integer size = (Integer) this.object(space + "RETRIEVE_DI_BY_NAME_SIZE", param, sqlMapClient);
		return size == null ? 0 : size;
	}
	public int retrieveDto2Size(Map<String, Object> param) {
		Integer size = (Integer) this.object(space + "RETRIEVE_SEX_AGE_SIZE", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	public void setCheckCategory(String id, String checkCategory, User user) {
		Map<String, Object> param = new HashMap<>();
		param.put("id", id);
		param.put("checkCategory", checkCategory);
		param.put("userId", user.getUserId());
		this.update(space + "SET_CHECK_CATEGORY", param, sqlMapClient);
	}

	public void updateCheckFlagByIndex(String diseaseName) {
		Map<String, Object> param = new HashMap<>();
		param.put("diseaseName", diseaseName);

		this.update(space + "UPDATE_CHECK_FLAG_BY_INDEX", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> retrieveListGroupBySexAge(List<String> diseaseNames) {

		boolean firstFlag = true;
		StringBuilder param = new StringBuilder();
		param.append("(");
		for (String name : diseaseNames) {
			if (firstFlag) {
				firstFlag = false;
			} else {
				param.append(",");
			}
			param.append("'");
			param.append(name);
			param.append("'");
		}
		param.append(")");

		return (List<String>) this.list(space + "RETRIEVE_LIST_GROUP_BY_SEX_AGE", param.toString(), sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> retrieveInitAgeSexData() {
		return (List<Map<String, Object>>) this.list(space + "RETRIEVE_INIT_AGE_SEX_DATA", sqlMapClient);
	}

	public void batchInsertAgeSex(List<IcdDiseaseDto> value) {
		Map<String, Object> param = new HashMap<>();
		int start = 0, end, size = value.size();
		List<IcdDiseaseDto> tempList;

		while (start < size) {
			if (start + 2000 < size) {
				end = start + 2000;
			} else {
				end = size;
			}
			tempList = value.subList(start, end);
			param.put("list", tempList);
			this.insert(space + "BATCH_INSERT_AGE_SEX", param, sqlMapClient);
			start += 2000;
		}
	}

	public void updateDiseaseId() {

		this.update(space + "UPDATE_DISEASE_ID", sqlMapClient);
	}

	public void updateUrgentCheckCategory(Map<String, String> update) {
		this.update(space + "UPDATE_URGENT_CHECK_CATEGORY", update, sqlMapClient);
	}

}
